﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Aidbrella.Web.Common;
using Aidbrella.Web.Models;
using Aidbrella.Web.ViewModels;
using AidbrellaWCFProxy.aidBrellaService;
using OrganizationType = Aidbrella.Web.Common.OrganizationType;

namespace Aidbrella.Web.Controllers
{
    public partial class ProjectController : BaseController
    {
        //
        // GET: /Project/
        private IList<LocationViewModel> locations = new List<LocationViewModel>()
    	                                      	{
    	                                      		new LocationViewModel()
    	                                      			{Latitude = 16.130262012034756, Longitude = 45.61523437499999},
    	                                      		new LocationViewModel()
    	                                      			{Latitude = 18.646245142670637, Longitude = 34.45312499999999}
    	                                      	};

        public virtual ActionResult ByLocation(double longitude, double latitude)
        {
        	return Json(new
        	            	{
        	            		points = locations.Select(item => new
        	            		                                  	{
        	            		                                  		lat = item.Latitude,
        	            		                                  		lon = item.Longitude
        	            		                                  	})
        	            	},
        	            JsonRequestBehavior.AllowGet);
        }

        public virtual ActionResult CreateNew()
        {
            return View(new CreateProjectViewModel());
        }

        public virtual ActionResult Show(int id)
        {
            AidBrellaServiceClient client = new AidBrellaServiceClient();
            Project project = client.GetProjectById(id);
            ProjectViewModel pvm = null;
            OrganizationViewModel orgVM = new OrganizationViewModel();
            if (project != null)
            {
                var orgs = client.GetOrganizations();

                pvm = new ProjectViewModel { 
                                              Title = project.Name,
                                              Description = project.Description,
                                              ProjectId = project.Id,
                                              ProjectLocations =  GetProjectLocations(client.GetSitesForProject(project.Id)),
                                              Organization = orgVM
                                           };

            }

            return View(pvm);
        }

        private List<ProjectSiteViewModel> GetProjectLocations(ProjectSite[] sites)
        {
            List<ProjectSiteViewModel> siteModels = new List<ProjectSiteViewModel>();

            foreach (var item in sites)
            {
                var siteModel = new ProjectSiteViewModel
                                    {
                                        Location = new LocationViewModel { Latitude = item.Latitude, Longitude = item.Longitude },
                                        ProjectId = item.ProjectId.Value
                                    };

                siteModels.Add(siteModel);
            }

            return siteModels;
        }

        [ChildActionOnly]
        public virtual ActionResult ProjectMemberList(int projectId)
        {
            UserViewModel first = new UserViewModel {
                Address = "Address",
                FirstName = "Testi",
                LastName = "Tyyppi",
                PersonId = 1
            };

            UserViewModel second = new UserViewModel
            {
                Address = "Address",
                FirstName = "Joku",
                LastName = "Hyypiö",
                PersonId = 2
            };

            ProjectMemberViewModel model = new ProjectMemberViewModel {
                Users = new List<UserViewModel> { first, second }
            };

            return View(model);
        }

        public virtual ActionResult GetProjects()
        {
            AidBrellaServiceClient client = new AidBrellaServiceClient();
            var model =  client.GetOrganizations();
            return View(model);
        }
    }
}
